import 'package:flutter/material.dart';

import '../../flutter_common.dart';
import '../../util/context_extension.dart';

class FxTextSpan extends TextSpan {
  FxTextSpan(
      {super.text, double? fontSize, Color? color, FontWeight? fontWeight})
      : super(
            style: TextStyle(
                fontSize: fontSize ?? fontSize24,
                color: color??Colors.black,
                fontWeight: fontWeight));
}

class BoldTextSpan extends FxTextSpan {
  BoldTextSpan({super.text, super.fontSize, super.color})
      : super(fontWeight: lightBold);
}

class FxText extends Text {
  FxText(super.text,
      {super.key, double? fontSize, Color? color, FontWeight? fontWeight})
      : super(
            style: TextStyle(
                fontSize: fontSize ?? fontSize24,
                color: color,
                fontWeight: fontWeight));
}

class BoldText extends FxText {
  BoldText(super.text, {super.key, super.fontSize, super.color})
      : super(fontWeight: lightBold);
}

/// Similar to TextButton
class ShapeText extends StatelessWidget {
  final String text;
  final EdgeInsetsGeometry? margin;
  final EdgeInsetsGeometry? padding;
  final BorderRadius? borderRadius;
  final Color? backgroundColor;
  final Color? borderColor;
  final double borderWidth;
  final double? fontSize;
  final FontWeight? fontWeight;
  final Color? color;
  final int? maxLine;
  final VoidCallback? onPressed;
  final VoidCallback? onLongPressed;
  final double? width;
  final double? height;
  final AlignmentGeometry? alignment;

  ///支持背景设置的 Text
  const ShapeText(this.text,
      {Key? key,
      this.margin,
      this.width,
      this.height,
      this.padding,
      this.borderRadius,
      this.borderColor,
        this.alignment,
        this.borderWidth = 1,
      this.backgroundColor,
      this.fontSize,
      this.color,
        this.fontWeight,
      this.maxLine,
      this.onPressed,
      this.onLongPressed})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget body = Text(
      text,
      maxLines: maxLine,
      overflow: maxLine == 1 ? TextOverflow.ellipsis : null,
      style: TextStyle(fontSize: fontSize, color: color, fontWeight: fontWeight),
    );
    Decoration? decoration;
    if (backgroundColor != null || borderColor != null) {
      Border? border = borderColor == null? null : Border.all(color: borderColor!, width: borderWidth);
      decoration =
          BoxDecoration(borderRadius: borderRadius, color: backgroundColor, border: border);
    }
    if(alignment != null) {
      body = Align(alignment: alignment!, child: body);
    }
    if (onPressed == null && onLongPressed == null) {
      return Container(
          padding: padding,
          margin: margin,
          width: width,
          height: height,
          decoration: decoration,
          child: body);
    } else {
      if (padding != null) body = Padding(padding: padding!, child: body);
      body = InkWell(
          onLongPress: onLongPressed,
          onTap: _tPressed,
          borderRadius: borderRadius,
          child: body);
      body = Material(color: Colors.transparent, child: Ink(decoration: decoration, height: height, width: width, child: body));
      return margin == null ? body : Padding(padding: margin!, child: body);
    }
  }

  _tPressed() => onPressed == null ? null : throttle(onPressed);
}

class FxCheckBox extends StatelessWidget {
  final bool? value;
  final String? text;
  final Widget? textWidget;
  ///It can be set to null to disable the checkbox
  final ValueChanged<bool?>? onChanged;
  final double? fontSize;
  final Color? textColor;
  final double borderWidth;
  final bool circleShape;
  final bool tristate;

  const FxCheckBox(
      {super.key,
      required this.value,
      this.text,
        this.textWidget,
      this.onChanged,
        this.tristate = false,
      this.fontSize,
      this.borderWidth = 0.5,
      this.circleShape = false,
      this.textColor});

  @override
  Widget build(BuildContext context) {
    Color primaryColor = Theme.of(context).primaryColor;
    if (text == null && textWidget == null) return _checkBox(context, primaryColor);

    return Stack(alignment: Alignment.centerLeft, children: [
      _checkBox(context, primaryColor),
      TextButton(
          onPressed: onChanged == null ? null : () => onChanged!(!value!),
          child: Padding(
              padding: const EdgeInsets.only(left: 30),
              child: textWidget??Text(text!,
                  style: TextStyle(
                      fontSize: fontSize,
                      color: textColor??primaryColor,
                      fontWeight: FontWeight.normal))))
    ]);
  }

  _checkBox(BuildContext context, Color pcolor) {
    return Checkbox(
        value: value,
        tristate: tristate,
        onChanged: onChanged,
        fillColor: MaterialStateProperty.resolveWith((states) => pcolor),
        shape: circleShape ? const CircleBorder() : null,
        side: BorderSide(width: borderWidth, color: textColor??Colors.black54));
  }
}
